<script setup lang="ts">
import { LaptopOutlined, NotificationOutlined, UserOutlined } from '@ant-design/icons-vue';
import { defineAsyncComponent, ref, watch } from 'vue';
import { useRoute } from 'vue-router';

import router from './app-router';
import AuthService from './authentication/auth-service';
import AuthenticationService from './shared/services/authentication-service';
import CommonService from './shared/services/common-service';
import useAuthenticationStore from './shared/stores/authentication-store';

const UserAvatar = defineAsyncComponent(() => import('./user/UserAvatar.vue'));
const authenticationStore = useAuthenticationStore();
const route = useRoute();

// We need to select the menu item when:
// - User saves authentication status and opens a url directly. Such like /user/list.
// - User is redirected to /user/list from login.
const selectedKeys = ref<string[]>([route.path]);
const openKeys = ref<string[]>(['/user']);

watch(() => route.path, (p) => {
  selectedKeys.value = [p];
});

function logout(): void {
  AuthService.logout();
  router.push('/authentication/login');
  authenticationStore.changeIsAuthenticated(AuthenticationService.isAuthenticated);
  authenticationStore.changeCurrentUser(AuthenticationService.currentUser);
  selectedKeys.value = ['/home'];
  openKeys.value = ['/user'];
}

function sideMenuClick(e: any) {
  router.push(e.key);
}
</script>

<template>
  <a-layout class="root-container">
    <a-layout-header class="header" v-if="authenticationStore.isAuthenticated">
      <img src="/assets/images/logo.png" class="logo" alt="logo" />
      <h3 class="header-title">平台管理系统</h3>
      <div class="header-user">
        <a-dropdown>
          <div @click.prevent>
            <UserAvatar :user="authenticationStore.currentUser!" />
          </div>
          <template #overlay>
            <a-menu>
              <a-menu-item>
                <div>修改资料</div>
              </a-menu-item>
              <a-menu-item>
                <div @click="logout">退出登录</div>
              </a-menu-item>
            </a-menu>
          </template>
        </a-dropdown>
      </div>
    </a-layout-header>
    <a-layout>
      <a-layout-sider width="200" v-if="authenticationStore.isAuthenticated">
        <a-menu v-model:selectedKeys="selectedKeys" v-model:openKeys="openKeys" @click="sideMenuClick" mode="inline"
          :style="{ height: '100%', borderRight: 0 }">
          <a-menu-item key="/home">
            <template #icon>
              <laptop-outlined />
            </template>
            <span>首页</span>
          </a-menu-item>
          <a-sub-menu key="/user">
            <template #title>
              <span>
                <user-outlined />
                用户
              </span>
            </template>
            <a-menu-item key="/user/list">列表</a-menu-item>
            <a-menu-item key="6">option6</a-menu-item>
            <a-menu-item key="7">option7</a-menu-item>
            <a-menu-item key="8">option8</a-menu-item>
          </a-sub-menu>
          <a-sub-menu key="sub3">
            <template #title>
              <span>
                <notification-outlined />
                subnav 3
              </span>
            </template>
            <a-menu-item key="9">option9</a-menu-item>
            <a-menu-item key="10">option10</a-menu-item>
            <a-menu-item key="11">option11</a-menu-item>
            <a-menu-item key="12">option12</a-menu-item>
          </a-sub-menu>
        </a-menu>
      </a-layout-sider>
      <a-layout :style="authenticationStore.isAuthenticated ? { padding: '0 24px 10px' } : {}">
        <a-layout-content class="site-layout-background"
          :style="authenticationStore.isAuthenticated ? { background: '#fff', padding: '24px', margin: 0, minHeight: '280px' } : {}">
          <router-view />
        </a-layout-content>
        <a-layout-footer class="footer" v-if="authenticationStore.isAuthenticated">
          Ant Design ©{{ CommonService.getCurrentDate().getFullYear() }} Created by Ant UED
        </a-layout-footer>
      </a-layout>
    </a-layout>
  </a-layout>
</template>

<style scoped>
.logo {
  width: 50px;
  height: 50px;
  margin-top: 6px;
}

.header {
  display: flex;
  flex-direction: row;
  color: #fff;
}

.header-title {
  color: #fff;
  margin-left: 10px;
}

.header-user {
  margin-left: auto;
}

.site-layout-background {
  background: #fff;
}

.root-container {
  min-height: 100vh;
}

.footer {
  text-align: left;
  padding: 10px 0px 0px 0px;
}
</style>